package com.step.ems.pages.setting

import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.step.ems.BuildConfig
import com.step.ems.R
import com.step.ems.common.Di
import com.step.ems.component.ItemInfo
import com.step.ems.component.MyDialog
import com.step.ems.component.MyTopBar
import com.step.ems.ui.theme.ContentBg
import com.step.ems.ui.theme.DarkBlue

@Composable
fun SettingScreen(
    viewModel: SettingViewModel = hiltViewModel(),
    toLogin: (closeStack: Boolean) -> Unit,
) {
    Scaffold(topBar = {
        MyTopBar(stringResource(R.string.setting))
    }) { padding ->
        var openDialog by remember { mutableStateOf(false) }
        MyDialog(
            openDialog = openDialog,
            onFirst = { openDialog = false },
            onSecond = {
                viewModel.logout()
                toLogin(true)
                openDialog = false
            },
            onDismissRequest = { openDialog = false },
            firstTextId = R.string.cancel,
            secondTextId = R.string.confirm,
            title = stringResource(R.string.logoutConfirm),
        )
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding)
                .background(color = ContentBg)
        ) {
            Spacer(modifier = Modifier.height(12.dp))
            SettingItem(R.string.username, viewModel.username)
            Spacer(modifier = Modifier.height(1.dp))
            SettingItem(R.string.host, Di.BASE_URL)
            Spacer(modifier = Modifier.height(12.dp))
            SettingItem(R.string.version, BuildConfig.VERSION_NAME)
            Button(
                modifier = Modifier
                    .padding(top = 50.dp)
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp)
                    .height(48.dp),
                shape = RoundedCornerShape(4.dp),
                colors = ButtonDefaults.buttonColors(containerColor = DarkBlue),
                onClick = {
                    if (viewModel.isLogin) openDialog = true else toLogin(false)
                }) {
                Text(
                    text = stringResource(id = if (viewModel.isLogin) R.string.logout else R.string.login),
                    fontSize = 16.sp
                )
            }
        }
    }
}

@Composable
fun SettingItem(
    @StringRes titleId: Int,
    value: String
) {
    ItemInfo(
        titleId,
        value,
        Modifier
            .background(Color.White)
            .padding(15.dp)
    )
}
